# 模糊隶属度计算
#计算了 max(bright,dark ) 一阶模糊距 阈值
import cv2

pic = cv2.imread("pic/a1.png", 0);
height, width= pic.shape
# 查看图片大小
print("height: ",height,"width: ",width)

hist = cv2.calcHist([pic], [0], None, [256], [0, 256])
# 查看灰度值频率
# for i in hist:
#     print(i);

# 求灰度值概率
p = []
for i in hist:
    p.append(i / (height * width))
# print(p) #输出概率

# custom.showPicture("test",pic);
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# 一阶模糊矩
m = 0
for i in range(256 - 1):
    m += (i / 255) * p[i];
print("一阶模糊矩为：", m)

#一层图像
u_dark, u_bright = [], []
a = 0.5
l = (1 - 2 * a) / (a * a)
for i in range(0, height):
    for j in range(0, width):
        pic_now = pic[i][j]/255
        if pic_now <= a:
            u_dark.append(1 / a * (pic_now ** 2))
        else:
            u_bright.append((1 - (1 / a * (((1 - pic_now) / (1 + l * pic_now)) ** 2))) / (1 + l * (1 / a * (((1 - pic_now) / (1 + l * pic_now)) ** 2))))

u_bright_max, u_dark_max = [max(u_bright)], [max(u_dark)]

print("u_bright_max: ",u_bright_max,"u_dark_max: ", u_dark_max)

if(m<=0.5):
    xgm=u_bright_max/2
else:
    xgm=u_dark_max/2
